﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using pay.Entity;
using pay.BLL;
using System.Collections;
using System.Text;

public partial class shopping11 : System.Web.UI.Page
{
    List<ShopCard> lst =new List<ShopCard>();
    public string str_products = "没有任何商品";
    int productid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {   
                
                if (Request["ProductID"].ToString().Equals("53"))
                {
                   
                    if (Session["EmoneyShopCard"]!=null)
                    {
                        lst = (List<ShopCard>)Session["EmoneyShopCard"];
                        List<int> tmplsit=new List<int>();
                        for (int i = 0; i < lst.Count; i++)
                        {
                            if (lst[i].ProductId == 52 || lst[i].ProductId == 36)
                            {
                                tmplsit.Add(lst[i].ProductId);
                            }
                        }
                        if (tmplsit.Count < 1)
                        {
                            Response.Write("<script>alert('此商品需搭配乾坤版单年版或双年版购买!');window.location.href='http://product.emoney.cn/'; window.opener = null;window.close();</script>");
                            return;
                        }
                        
                    }
                    else
                    {

                        Response.Write("<script>alert('此商品需搭配乾坤版单年版或双年版购买!');window.location.href='http://product.emoney.cn/'; window.opener = null;window.close();</script>");
                        return;
                    }


                }// if 用户准备购买一元赠品
                

            }
            catch
            {
               
            }

            if (!string.IsNullOrEmpty(Request["UserID"]))
            {
                Session["UserID"] = Request["UserID"].ToString();
            }

            Buy_Product();
            str_products = getList().ToString();

            if (!string.IsNullOrEmpty(Request["ProductID"]))
            {
                Response.Redirect(Request.Path);
            }
            

        }
    }

    ShopCard sc =new ShopCard();
    public void  Buy_Product()
    {
        if (string.IsNullOrEmpty(Request["ProductID"]))
        {
            if (Session["EmoneyShopCard"] == null)
            {
                Response.Redirect("shopping0.aspx");
            }
            else
            {
                return;
            }
        }

        if (!int.TryParse(Request["ProductID"], out productid))
        {

          // Response.Redirect("http://product.emoney.cn/product.aspx");
           Response.Redirect("http://product.emoney.cn/");
          // Response.Redirect("product.aspx");
        }
        //if (bll.IsProductid(Request["uid"]) == false)
        //{
        //    // Response.Redirect("http://www.emoney.cn/product/index.html");
        //    Response.Redirect("product.aspx");
        //}
        if (Session["EmoneyShopCard"] == null)
        {
            try
            {
                lst = new List<ShopCard>();
                sc = new ShopCard();
                sc.Count = 1;
                sc.ProductId = productid;
                lst.Add(sc);
                Session["EmoneyShopCard"] = lst;
            }
            catch(Exception ex)
            {
                Response.Write("<script>alert('"+ex.Message+"'); this.history.back(-1);</script>");
            }

        }

        else
        {


            lst = (List<ShopCard>)Session["EmoneyShopCard"];
            bool flag = true;
            
            for (int i = 0; i < lst.Count; i++)
            {
                sc = lst[i];
                if (productid == sc.ProductId)
                {
                    sc.Count += 1;  //数量
                    flag = false;
                }
            }
            if (flag)
            {
                sc = new ShopCard();
                sc.Count = 1;
                sc.ProductId = productid;
                lst.Add(sc);
            }
            Session["EmoneyShopCard"] = lst;

        }

    }
    public double sum = 0;
    ProductBLL bll = new ProductBLL();
    public StringBuilder getList()
    {
        StringBuilder sb = new StringBuilder();
        if (Session["EmoneyShopCard"] != null)
        {
            lst = (List<ShopCard>)Session["EmoneyShopCard"];
        }
        if (lst == null) 
        {
            Response.Redirect("shopping0.aspx");
        }
        foreach (var c in lst)
        {
            Product product = bll.GetElementById(c.ProductId);
            try
            {
                c.ProductName = product.Productname;
                c.Price = product.Nowprice.Value;
                sum += c.Amount;
                sb.AppendLine("<ul class=\"product_zong\">");
                sb.AppendLine("<li class=\"product_zong1\">" + c.ProductName + "</li>");
                sb.AppendLine("<li class=\"product_zong2\" id=\"price\">" + c.Price + "元</li>");
                sb.AppendLine("<li class=\"product_zong2 product_zong22  \">");
                sb.AppendLine("<table width=\"230\" border=\"0\" class=\"bdxuan\">");
                sb.AppendLine("<tr>");
                sb.AppendLine("<td  class=\"bdxuan_xiao\">  <input name=\"\"  type=\"button\" class=\"anniu_left \" onclick=\"xun_jian(" + c.ProductId + ")\"/></td>");
                sb.AppendLine("<td  class==\"bdxuan_z\"><input  id=\"counts\"  price=\"" + c.Price + "\" pid=\"" + c.ProductId + "\" name=\"counts\"  type=\"text\" value=\"" + c.Count + "\"  class=\"xuanjian\"/></td>");
                sb.AppendLine("<td class=\"bdxuan_xiao\"><input name=\"\"  type=\"button\" class=\"anniu_right\"  onclick=\"xun_zeng(" + c.ProductId + ")\"/></td>");
                sb.AppendLine("<td class=\"bdxuan_xiao\">   件</td>");
                sb.AppendLine("<td id=\"ck_count" + c.ProductId + "\" name=\"ck_count\"  class=\"bdxuan_zhong\"></td>");
                sb.AppendLine("</tr>");
                sb.AppendLine("</table>");
                sb.AppendLine("</li>");
                sb.AppendLine("<li class=\"product_zong3\" id=\"amount" + c.ProductId + "\">" + c.Amount + "元</li>");
                sb.AppendLine("<li class=\"product_zong4\"><img  pid='" + c.ProductId + "' id=\"del\"  src=\"images/delete.gif\" onclick=\"deleteTr(this)\"/></li>");
                sb.AppendLine("</ul>");


            }
            catch (Exception ex)
            {
                
                sb.AppendLine("系统错误: " + ex.Message);
            }

        }
        return sb;


    }

  

}